//
//  XJ_ReturnListViewController.m
//  XunJieProject
//
//  Created by 薛子 on 2018/6/12.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_ReturnListViewController.h"
#import "XJ_returnListCell.h"
#import "XJ_ReturnListCellModel.h"
#import "XJ_ReturnListContentModel.h"
#import "BC_KeFuAlextView.h"

@interface XJ_ReturnListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    XJ_ReturnListContentModel * contentModel ;
    NSInteger flag;// 1 全部  2 已执行 1 未执行
}
@property (weak, nonatomic) IBOutlet UILabel *planNoLabel;
@property (weak, nonatomic) IBOutlet ZDXXibLabel *planCreateTimeLabel;

@property (weak, nonatomic) IBOutlet UILabel *alreadyBackMoneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *waitBackMoneyLabel;

@property (weak, nonatomic) IBOutlet UILabel *consumeMoneyLabel;

@property (weak, nonatomic) IBOutlet UITableView *listTable;

@property (nonatomic, strong)NSMutableArray * planDetailListArray;

@property (nonatomic, strong)NSMutableArray * executeArray;//已执行

@property (nonatomic, strong)NSMutableArray * noexecuteArray;//未执行
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewLead;

@property (weak, nonatomic) IBOutlet UIButton *allButton;

@property (weak, nonatomic) IBOutlet UIButton *executeButton;
@property (weak, nonatomic) IBOutlet UIButton *noexecuteButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewTop;

@end

@implementation XJ_ReturnListViewController


- (NSMutableArray *)executeArray{
    
    if (_executeArray == nil) {
        
        _executeArray = [NSMutableArray array];
        
    }
    
    return _executeArray;
}

- (NSMutableArray *)noexecuteArray{
    
    if (_noexecuteArray == nil) {
        
        _noexecuteArray = [NSMutableArray array];
    }
    
    return _noexecuteArray;
}
- (NSMutableArray *)planDetailListArray{
    
    if (_planDetailListArray == nil) {
        
        _planDetailListArray = [NSMutableArray array];
    }
    
    return _planDetailListArray;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 开启返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_titleLabel.text = @"返还列表";
    flag = 1;
    
    _viewTop.constant = SafeAreaTopHeight;
    
    [self requestPlanListData];
    
    
    self.nav_rightButton.hidden = NO;
    [self.nav_rightButton setTitle:@"结束计划" forState:UIControlStateNormal];
    [self.nav_rightButton addTarget:self action:@selector(endThePlanAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _listTable.delegate = self;
    _listTable.dataSource = self;
    _listTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    _listTable.layer.cornerRadius = 8;
    
    // Do any additional setup after loading the view from its nib.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (flag == 1) {
        
       return self.planDetailListArray.count;
        
    }else if (flag == 2){
        
        return self.executeArray.count;
        
    }else if (flag == 3){
        
        return  self.noexecuteArray.count;
        
    }else{
        
        return 0;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XJ_returnListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"XJ_returnListCell"];

    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"XJ_returnListCell" owner:self options:nil] objectAtIndex:0];
    }
    
    XJ_ReturnListCellModel * model;
    
    if (flag == 1) {
        
        model= [self.planDetailListArray objectAtIndex:indexPath.row];
        
    }else if (flag == 2){
        
        model= [self.executeArray objectAtIndex:indexPath.row];
        
    }else if (flag == 3){
        
        model= [self.noexecuteArray objectAtIndex:indexPath.row];
    }
   
    
    [cell setDataForCell:model];
    
    return  cell;
}

- (void)requestPlanListData{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:USERID forKey:@"userId"];
    
    if (token!=nil &&![token isEqualToString:@""]) {
        
        [dic setValue:token forKey:@"token"];
        
    }
    
    
    [dic setValue:_planID forKey:@"planId"];
    
    
    [XJ_Utils signPost:@"plan/list?" parameters:dic success:^(NSInteger recode, id jsonObject) {
        
        [self.planDetailListArray removeAllObjects];
        [self.executeArray removeAllObjects];
        [self.noexecuteArray removeAllObjects];
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        if (dataDic.count> 0) {
            
            self->contentModel = [XJ_ReturnListContentModel mj_objectWithKeyValues:dataDic];
            
            [self setContentData];
            
            NSArray *planDetailListArr = [dataDic objectForKey:@"planDetailList"];
            if (planDetailListArr.count > 0) {
                
                self.listTable.hidden = NO;
                
                for (NSDictionary * dic in planDetailListArr) {
                    
                    XJ_ReturnListCellModel * cellModel = [XJ_ReturnListCellModel mj_objectWithKeyValues:dic];
                    
                    [self.planDetailListArray addObject:cellModel];
                    
                    if (cellModel.executeFlag == 0) {
                        
                        [self.noexecuteArray addObject:cellModel];
                        
                    }else if (cellModel.executeFlag == 1){
                        
                        [self.executeArray addObject:cellModel];
                    }
                    
                }
            }else{
                
                self.listTable.hidden = YES;
            }
        }
       
        [self.listTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

- (void)setContentData{
    
    _planNoLabel.text = contentModel.planNo;
    _planCreateTimeLabel.text = contentModel.planCreateTime;
    _alreadyBackMoneyLabel.text = [NSString stringWithFormat:@"￥%0.2f",[contentModel.alreadyBackMoney floatValue]];
    _waitBackMoneyLabel.text =  [NSString stringWithFormat:@"￥%0.2f",[contentModel.waitBackMoney floatValue]];
    _consumeMoneyLabel.text =  [NSString stringWithFormat:@"￥%0.2f",[contentModel.consumeMoney floatValue]];
    
}
- (void)endThePlanAction:(UIButton *)sender{
    
    BC_KeFuAlextView *alext =[[BC_KeFuAlextView alloc]initWithMessage:@"提示" sureBtn:@"确定" cancleBtn:@"取消" phone:@"确定结束计划吗？" delete:YES];
    alext.resultIndex = ^(NSInteger index) {
        
        NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
        NSString * token = [db objectForKey:@"token"];
        
        NSMutableDictionary * dic = [NSMutableDictionary dictionary];
        [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
        [dic setValue:APPVERSION forKey:@"appVersion"];
        [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
        [dic setValue:USERID forKey:@"userId"];
        
        if (token!=nil &&![token isEqualToString:@""]) {
            
            [dic setValue:token forKey:@"token"];
            
        }
        [dic setValue:self.planID forKey:@"planId"];
        
        [XJ_Utils signPost:@"plan/end?" parameters:dic success:^(NSInteger recode, id jsonObject) {
            
            ZDXAlertView *alext =[[ZDXAlertView alloc]initWithMessage:@"结束计划成功" image:@"pop_msg_tip_xs"];
            [alext showXLAlertView];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
            
            
        } failure:^(NSInteger recode, NSError *error) {
            
        }];
        
    };
    [alext showXLAlertView];
    
    
    
    
}

- (IBAction)allListAction:(id)sender {
    
    [_allButton setTitleColor:[UIColor colorWithHexString:@"FA832F"] forState:UIControlStateNormal];
    [_executeButton setTitleColor:[UIColor colorWithHexString:@"A9AAAB"] forState:UIControlStateNormal];
    [_noexecuteButton setTitleColor:[UIColor colorWithHexString:@"A9AAAB"] forState:UIControlStateNormal];
    
    
    _lineViewLead.constant = 0;
    
    flag = 1;
    
    if (self.planDetailListArray.count > 0) {
        
        self.listTable.hidden  = NO;
        [self.listTable reloadData];
        
    }else{
        
        self.listTable.hidden = YES;
    }
    
}


- (IBAction)executeAction:(id)sender {
    
    [_executeButton setTitleColor:[UIColor colorWithHexString:@"FA832F"] forState:UIControlStateNormal];
    [_allButton setTitleColor:[UIColor colorWithHexString:@"A9AAAB"] forState:UIControlStateNormal];
    [_noexecuteButton setTitleColor:[UIColor colorWithHexString:@"A9AAAB"] forState:UIControlStateNormal];
    _lineViewLead.constant = SCREEN_WIDTH/3;
    
    flag = 2;
    
    if (self.executeArray.count > 0) {
        
        self.listTable.hidden  = NO;
         [self.listTable reloadData];
        
    }else{
        
        self.listTable.hidden = YES;
    }
   
}
- (IBAction)noexecuteAction:(id)sender {
    
    [_allButton setTitleColor:[UIColor colorWithHexString:@"A9AAAB"] forState:UIControlStateNormal];
    [_executeButton setTitleColor:[UIColor colorWithHexString:@"A9AAAB"] forState:UIControlStateNormal];
    [_noexecuteButton setTitleColor:[UIColor colorWithHexString:@"FA832F"] forState:UIControlStateNormal];
    
    _lineViewLead.constant = SCREEN_WIDTH/3 * 2;
    
    flag = 3;
    
    if (self.noexecuteArray.count > 0) {
        self.listTable.hidden  = NO;
        [self.listTable reloadData];
    }else{
        
        self.listTable.hidden  = YES;
    }
    
}
- (void)backButtonClick{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
